package com.moioo.exam;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for TMenu
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.moioo.exam.TMenu
 * @author MyEclipse Persistence Tools
 */

public class TMenuDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(TMenuDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String PID = "pid";
	public static final String LEV = "lev";
	public static final String HREF = "href";
	public static final String FUNC = "func";
	public static final String ORDISPLAY = "ordisplay";

	public void save(TMenu transientInstance) {
		log.debug("saving TMenu instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TMenu persistentInstance) {
		log.debug("deleting TMenu instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TMenu findById(java.lang.Integer id) {
		log.debug("getting TMenu instance with id: " + id);
		try {
			TMenu instance = (TMenu) getSession().get("com.moioo.exam.TMenu",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(TMenu instance) {
		log.debug("finding TMenu instance by example");
		try {
			List results = getSession().createCriteria("com.moioo.exam.TMenu")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TMenu instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TMenu as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByPid(Object pid) {
		return findByProperty(PID, pid);
	}

	public List findByLev(Object lev) {
		return findByProperty(LEV, lev);
	}

	public List findByHref(Object href) {
		return findByProperty(HREF, href);
	}

	public List findByFunc(Object func) {
		return findByProperty(FUNC, func);
	}

	public List findByOrdisplay(Object ordisplay) {
		return findByProperty(ORDISPLAY, ordisplay);
	}

	public List<TMenu> findAll() {
		log.debug("finding all TMenu instances");
		try {
			String queryString = "from TMenu t order by t.pid asc,t.ordisplay asc";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TMenu merge(TMenu detachedInstance) {
		log.debug("merging TMenu instance");
		try {
			TMenu result = (TMenu) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TMenu instance) {
		log.debug("attaching dirty TMenu instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TMenu instance) {
		log.debug("attaching clean TMenu instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}